"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getSubDir = exports.getAppDataDir = exports.getHomeDir = exports.isWindows = exports.getProjectDir = exports.getEnv = exports.getPort = exports.generoResponse = void 0;
const fs_1 = __importDefault(require("fs"));
const os_1 = __importDefault(require("os"));
const path_1 = require("path");
/** 通用返回消息体 */
function generoResponse(data, errorMsg) {
    return {
        success: !errorMsg,
        data,
        errorMsg,
    };
}
exports.generoResponse = generoResponse;
/** 获取端口 */
function getPort() {
    var _a, _b;
    const args = process.argv.slice(2);
    const PORT = (_b = (_a = args.find((item) => item.includes("PORT="))) === null || _a === void 0 ? void 0 : _a.replace("PORT=", "")) !== null && _b !== void 0 ? _b : 9000;
    return PORT;
}
exports.getPort = getPort;
/** 获取运行环境 */
function getEnv() {
    var _a, _b;
    const args = process.argv.slice(2);
    const ENV = (_b = (_a = args.find((item) => item.includes("ENV="))) === null || _a === void 0 ? void 0 : _a.replace("ENV=", "")) !== null && _b !== void 0 ? _b : "development";
    return ENV;
}
exports.getEnv = getEnv;
/**
 * 获取当前项目根目录
 * @description 测试环境为了模拟，返回project测试项目
 */
function getProjectDir() {
    const env = getEnv();
    const processDir = process.cwd();
    return env === "development" ? (0, path_1.join)(processDir, "../project") : processDir;
}
exports.getProjectDir = getProjectDir;
/**
 * 是否Window系统
 */
function isWindows() {
    return /^win/i.test(process.platform);
}
exports.isWindows = isWindows;
/**
 * 当前系统用户文件目录
 */
function getHomeDir() {
    return os_1.default.homedir();
}
exports.getHomeDir = getHomeDir;
/**
 * 插件临时文件、缓存等存放目录
 */
function getAppDataDir() {
    // 文件夹以点号开头，用于区分自动生成的目录（不建议用户手动修改），同时在 Linux/Unix 系统中代表隐藏
    const dir = (0, path_1.join)(os_1.default.homedir(), ".umi-plugins-szworks");
    if (!fs_1.default.existsSync(dir)) {
        fs_1.default.mkdirSync(dir);
    }
    return dir;
}
exports.getAppDataDir = getAppDataDir;
/**
 * 获取子目录列表（不包含子级）
 * @param path 路径
 * @returns 目录列表
 */
function getSubDir(path) {
    const files = fs_1.default.readdirSync(path);
    const dirList = [];
    files.forEach((item) => {
        const fPath = (0, path_1.join)(path, item);
        const stat = fs_1.default.statSync(fPath);
        if (stat.isDirectory()) {
            dirList.push(item);
        }
    });
    return dirList;
}
exports.getSubDir = getSubDir;
